<?php

class ClientsDAO {

    public function __construct($db) {

        $this->db = $db;
    }

    public function create(ContactCli $cli) {
        $db = $this->db;

        $stmt = $db->prepare("INSERT INTO `clients` (name,phone,birth,cadastre,cpf,sexo) VALUES (?,?,?,?,?,?)");
        
        $name = $cli->getName();
        $stmt->bindParam(1, $name);

        $phone = $cli->getPhone();
        $stmt->bindParam(2, $phone);

        $birth = $cli->getBirth();
        $stmt->bindParam(3, $birth);

        $dateCad = $cli->getDatCad();
        $stmt->bindParam(4, $dateCad);

        $doc = $cli->getDoc();
        $stmt->bindParam(5, $doc);

        $sexo = $cli->getSex();
        $stmt->bindParam(6, $sexo);

        $result = $stmt->execute();
        if ($result) {
            return true;
        }else
            return 0;
    }

    public function update(ContactCli $cli) {
        $db = $this->db;
        $id = $cli->getId();
        $stmt = $db->prepare("update clients c set c.name=?,c.phone=?,c.birth=?,
                    c.cadastre=?,c.cpf=?,c.sexo=? where c.id=$id");

        $name = $cli->getName();
        $stmt->bindParam(1, $name);

        $phone = $cli->getPhone();
        $stmt->bindParam(2, $phone);

        $birth = $cli->getBirth();
        $stmt->bindParam(3, $birth);

        $dateCad = $cli->getDatCad();
        $stmt->bindParam(4, $dateCad);

        $doc = $cli->getDoc();
        $stmt->bindParam(5, $doc);

        $sexo = $cli->getSex();
        $stmt->bindParam(6, $sexo);

        $result = $stmt->execute();
        if ($result) {
            return true;
        }else
            return 0;
    }

    public function remove($id) {
        $db = $this->db;


        $stmt = $db->prepare("delete from clients  where id=$id");
        $result = $stmt->execute();
        if ($result) {
            return true;
        }else
            return 0;
    }

    public function getById($id) {
        $contactCli = null;
        $db = $this->db;

        $stmt = $db->prepare("SELECT * FROM clients WHERe id = ?");
        $stmt->bindParam(1, $id);
        $stmt->execute();

        $row = $stmt->fetch();

        if (isset($row['id'])) {
            $cli = new ContactCli();
            $cli->setId($row['id']);
            $cli->setName($row['name']);
            $cli->setPhone($row['phone']);
            $birth = ($row['birth']);
            $birth2 = $this->convertsDate2($birth);
            $cli->setBirth($birth2);
            $cli->setDatCad($row['cadastre']);
            $cli->setDoc($row['cpf']);
            $cli->setSex($row['sexo']);

            $contactCli = $cli;
        }

        return $contactCli;
    }

    public function getByDoc($doc) {
        $contactCli = null;
        $db = $this->db;

        $stmt = $db->prepare("SELECT * FROM clients WHERe cpf = ?");
        $stmt->bindParam(1, $doc);
        $stmt->execute();

        $row = $stmt->fetch();

        if (isset($row['cpf'])) {
            $cli = new ContactCli();
            $cli->setId($row['id']);
            $cli->setName($row['name']);
            $cli->setPhone($row['phone']);
            $birth = ($row['birth']);
            $birth2 = $this->convertsDate2($birth);
            $cli->setBirth($birth2);
            $cli->setDatCad($row['cadastre']);
            $cli->setDoc($row['cpf']);
            $cli->setSex($row['sexo']);

            $contactCli = $cli;
        }

        return $contactCli;
    }

    public function getList($name) {


        $contactsCli = array();
        $db = $this->db;

        $stmt = $db->prepare("SELECT * FROM clients where name like'%$name%'");
        $stmt->execute();
        $result = $stmt->fetchAll();
        if (sizeof($result) > 0) {
            foreach ($result as $row) {

                $cli = new ContactCli();
                $cli->setId($row['id']);
                $cli->setName($row['name']);
                $cli->setPhone($row['phone']);
                $birth = ($row['birth']);
                $birth2 = $this->convertsDate2($birth);
                $cli->setBirth($birth2);
                $cadastre = ($row['cadastre']);
                $cadastre2 = $this->convertsDate3($cadastre);
                $cli->setDatCad($cadastre2);
                $cli->setDoc($row['cpf']);
                $cli->setSex($row['sexo']);



                $contactsCli[] = $cli;
            }

            return $contactsCli;
        }
    }

    public function convertsDate2($data) {
        if (!strstr($data, '/')) {
            // $data est� no formato ISO (yyyy-mm-dd) e deve ser convertida
            // para dd/mm/yyyy
            sscanf($data, '%d-%d-%d', $y, $m, $d);
            return sprintf('%d/%d/%d', $d, $m, $y);
        } else {
            // $data est� no formato brasileiro e deve ser convertida para ISO
            sscanf($data, '%d/%d/%d', $d, $m, $y);
            return sprintf('%d-%d-%d', $y, $m, $d);
        }

        return false;
    }

    public function convertsDate3($data) {
        if (!strstr($data, '/')) {
            // $data est� no formato ISO (yyyy-mm-dd) e deve ser convertida
            // para dd/mm/yyyy
            sscanf($data, '%d-%d-%d %d:%d:%d', $y, $m, $d, $H, $i, $s);
            return sprintf('%d/%d/%d %d:%d:%d', $d, $m, $y, $H, $i, $s);
        } else {
            // $data est� no formato brasileiro e deve ser convertida para ISO
            sscanf($data, '%d-%d-%d %d:%d:%d', $d, $m, $y, $H, $i, $s);
            return sprintf('%d-%d-%d %d:%d:%d', $y, $m, $d, $H, $i, $s);
        }

        return false;
    }

}

?>
